package com.cqc.cameraphoto01;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.FileUtils;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private Button btn1;
    private Button btn2;

    public static final String PICTURE_FILE = "temp.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) { //获取缩略图
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");//将数据转换成bitmap
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg";
            File oldFile = BitmapUtil.saveBitmapToFile(path, bitmap);

            //缩略图没有拍照时间
            String TAG_DATETIME = "";
            try {
                ExifInterface exif = new ExifInterface(path);
                TAG_DATETIME = exif.getAttribute(ExifInterface.TAG_DATETIME);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "path=" + path + ",file.getPath=" + oldFile.getPath() + ",AbsolutePat" + oldFile.getAbsolutePath() + ",length=" + oldFile.length()+",TAG_DATETIME="+TAG_DATETIME);
        } else if (requestCode == 2) { //获取原图
            String path2 = Environment.getExternalStorageDirectory() + "/ABC/" + PICTURE_FILE;
            File oldFile2 = new File(path2);

            
            //原有拍照时间
            String TAG_DATETIME = "";
            try {
                ExifInterface exif = new ExifInterface(path2);
                 TAG_DATETIME = exif.getAttribute(ExifInterface.TAG_DATETIME);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d(TAG, oldFile2.getPath() + ",AbsolutePat" + oldFile2.getAbsolutePath() + ",length=" + oldFile2.length()+",TAG_DATETIME="+TAG_DATETIME);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent1, 1);
                break;
            case R.id.btn2:
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                String path = Environment.getExternalStorageDirectory() + "/ABC/" + PICTURE_FILE;
                boolean orExistsFile = FileUtils.createOrExistsFile(path);
                if (orExistsFile) {
                    File file = new File(path);
                    Uri imageUri = Uri.fromFile(file);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(intent, 2);
                } else {
                    Log.d(TAG, "创建文件失败");
                }
                break;
        }
    }
}
